package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tebiecloud.base.client.constants.BaseConstants;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.order.client.model.entity.OrderContactLog;
import com.tebiecloud.order.client.model.entity.OrderGoods;
import com.tebiecloud.order.client.model.entity.Orderr;
import com.tebiecloud.order.server.service.OrderContactLogService;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.order.server.service.OrderGoodsService;
import com.tebiecloud.order.server.service.OrderrService;
import com.tebiecloud.order.server.service.feign.StoreStaffServiceClient;
import com.tebiecloud.store.client.model.entity.StoreStaff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *  前端控制器
 * @author tebie
 * @date 2019-09-27
 */
@RestController
@RequestMapping("/ordercontactlog")
public class OrderContactLogController {

    @Autowired
    private OrderContactLogService orderContactLogService;

    @Autowired
    private OrderrService orderrService;

    @Autowired
    private OrderGoodsService orderGoodsService;

    @Autowired
    private StoreStaffServiceClient storeStaffServiceClient;

    /**
     * 添加数据
     * @param orderId 订单id,
     * @param phone 手机号
     * @return
     */
    @PostMapping("call")
    public ResultBody add(
        @RequestParam(value = "orderId") Long orderId,
        @RequestParam(value = "phone") String phone) {
        //查询订单信息
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id",orderId);
        orderGoodsQueryWrapper.last("LIMIT 1");
        OrderGoods orderGoods = orderGoodsService.getOne(orderGoodsQueryWrapper);
        //用户信息
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        //B端拨打
        if(BaseConstants.ACCOUNT_DOMAIN_USER.equalsIgnoreCase(securityUserDetails.getDomain())) {
            OrderContactLog entity = new OrderContactLog();
            entity.setOrderId(orderId);
            entity.setUserId(securityUserDetails.getUserId());
            entity.setPhone(phone);
            //查询员工信息
            ResultBody<StoreStaff> storeStaffResultBody = storeStaffServiceClient.selectStoreStaff(orderGoods.getStoreId(),securityUserDetails.getUserId(),null);
            if (storeStaffResultBody.getCode() == ResultEnum.OK.getCode() && storeStaffResultBody.getData() != null) {
                StoreStaff storeStaff = storeStaffResultBody.getData();
                if(storeStaff!=null)entity.setStaffName(storeStaff.getName());
            }
            orderContactLogService.save(entity);
        }
        return ResultBodyUtils.ok();
    }

    /**
     * 获取会员通话记录
     * @param page
     * @param limit
     * @param userId
     * @return
     */
    @PostMapping("listcontact")
    public ResultBody listcontact(
            @RequestParam(value = "page",defaultValue = "1") int page,
            @RequestParam(value = "limit",defaultValue = "10") int limit,
            @RequestParam(value = "userId") long userId){
        //先查询所有订单
        QueryWrapper<Orderr> orderrQueryWrapper = new QueryWrapper<>();
        orderrQueryWrapper.eq("user_id",userId);
        List<Orderr> orderrList = orderrService.list(orderrQueryWrapper);
        List<Long> orderIdList = new ArrayList<>();
        if(orderrList!=null && orderrList.size()>0){
            for(Orderr orderr:orderrList){
                orderIdList.add(orderr.getOrderId());
            }
        }
        IPage<OrderContactLog> orderContactLogIPage = new Page<>();
        if(orderIdList!=null && orderIdList.size()>0){
            QueryWrapper<OrderContactLog> orderContactLogQueryWrapper = new QueryWrapper<>();
            orderContactLogQueryWrapper.in("order_id",orderIdList);
            orderContactLogQueryWrapper.orderByDesc("ct");
            orderContactLogIPage = orderContactLogService.page(new Page<OrderContactLog>(page,limit),orderContactLogQueryWrapper);
        }
        return ResultBodyUtils.ok(orderContactLogIPage);
    }
}
